package com.csg.web.controller;

import com.csg.web.dto.User;
import com.csg.web.dto.UserQueryCondition;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户接口
 *
 * @author george
 */
@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {

    /**
     * 获取当前登录用户
     *
     * @param principal 登录用户
     * @return 登录用户
     */
    @GetMapping(value = "/me")
    @ApiOperation(value = "获取当前登录用户")
    public Principal user(Principal principal) {
        return principal;
    }

    /**
     * 用户查询请求
     *
     * @param condition 查询条件
     * @param pageable  分页组件
     * @return 用户信息列表
     */
    @GetMapping
    @JsonView(User.UserSimpleView.class)
    @ApiOperation(value = "用户查询服务")
    public List<User> query(UserQueryCondition condition, @PageableDefault(page = 2, size = 20, sort = {"username", "password"}) Pageable pageable) {

        log.info("condition: " + ReflectionToStringBuilder.reflectionToString(condition, ToStringStyle.JSON_STYLE));
        log.info("page size: " + pageable.getPageSize());
        log.info("page number: " + pageable.getPageNumber());
        log.info("sort: " + pageable.getSort());

        List<User> users = new ArrayList<>();
        users.add(new User());
        users.add(new User());
        users.add(new User());
        return users;
    }

    /**
     * 获取用户信息
     *
     * @param id id信息
     * @return 用户信息
     */
    @GetMapping(value = "/{id:\\d+}")
    @JsonView(User.UserDetailView.class)
    @ApiOperation(value = "获取用户信息")
    public User getInfo(@ApiParam(value = "用户ID") @PathVariable String id) {
        User user = new User();
        user.setUsername("user");
        return user;
    }

    /**
     * 用户创建请求
     *
     * @param user   用户信息
     * @param result 错误校验信息
     * @return 创建成功的用户信息
     */
    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult result) {

        if (result.hasErrors()) {
            result.getAllErrors().forEach(objectError -> log.error(objectError.getDefaultMessage()));
        }
        log.info("user: " + ReflectionToStringBuilder.reflectionToString(user, ToStringStyle.JSON_STYLE));
        user.setId(1L);
        return user;
    }

    /**
     * 用户修改请求
     *
     * @param user   用户信息
     * @param result 错误校验信息
     * @return 修改成功的用户信息
     */
    @PutMapping(value = "/{id:\\d+}")
    public User update(@Valid @RequestBody User user, BindingResult result) {
        if (result.hasErrors()) {
            result.getAllErrors().forEach(objectError -> {
                String errorMessage = "";
                if (objectError instanceof FieldError) {
                    FieldError fieldError = (FieldError) objectError;
                    errorMessage = fieldError.getField() + ": " + fieldError.getDefaultMessage();
                } else {
                    errorMessage = objectError.getDefaultMessage();
                }
                log.error(errorMessage);
            });
        }
        log.info("user: " + ReflectionToStringBuilder.reflectionToString(user, ToStringStyle.JSON_STYLE));
        return user;
    }

    /**
     * 用户删除请求
     *
     * @param id 用户id
     */
    @DeleteMapping(value = "/{id:\\d+}")
    public void delete(@PathVariable String id) {
        log.info("id: " + id);
    }
}
